package com.huaxin.device.mapping;

import com.huaxin.device.dto.enums.DeviceStatus;
import com.huaxin.device.dto.request.AddDeviceInstanceReqDTO;
import com.huaxin.device.dto.request.UpdateDeviceInstanceReqDTO;
import com.huaxin.device.models.DeviceInstance;
import com.huaxin.hxenum.enums.DeviceType;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper(componentModel = "spring")
public interface DeviceInstanceMapping {
    @Mapping(target = "deviceType", ignore = true)
    DeviceInstance toEntity(AddDeviceInstanceReqDTO in);

    @AfterMapping
    default void afterMapping(AddDeviceInstanceReqDTO in, @MappingTarget DeviceInstance entity) {
        entity.setDeviceType(DeviceType.of(in.getDeviceType()));
    }

    @Mapping(target = "deviceType", ignore = true)
    @Mapping(target = "status", ignore = true)
    void updateEntity(UpdateDeviceInstanceReqDTO in, @MappingTarget DeviceInstance entity);

    @AfterMapping
    default void afterMapping(UpdateDeviceInstanceReqDTO in, DeviceInstance entity) {
        entity.setDeviceType(DeviceType.of(in.getDeviceType()));
        entity.setStatus(DeviceStatus.of(in.getStatus()));
    }
}